// pages/smartlife/bus/busline/buslinemap/buslinemap.js
var common = require('../../../../../utils/common.js');
var width = 320
var height = 480
var needRefresh = false
Page({
  data: {
    busRouteDetail: {},
    height: 0,
    showLocation: true,
    latitude: 0,
    longitude: 0,
    nearStationList: [],
    markers: [],
    controls: [],
    polyline: [],
    upDown: '',
    lineId: '',
    lineName: ''
  },
  onShareAppMessage: function () {
    var that = this
    return {
      title: '我在' + that.data.lineName,
      path: '/pages/smartlife/bus/busline/buslinemap/buslinemap?upDown=' + that.data.upDown + '&lineId=' + that.data.lineId + '&lat=' + that.data.latitude + '&lgt=' + that.data.longitude + '&lineName=' + that.data.lineName
    }
  },
  regionchange(e) {
    var that = this
    // console.log(e.type)
    that.setData({
      controls: [{
        id: 100,
        iconPath: '/image/location_notcenter.png',
        position: {
          left: width - 40,
          top: height - 50,
          width: 30,
          height: 30
        },
        clickable: true
      },
      {
        id: 101,
        iconPath: '/image/refresh.png',
        position: {
          left: width - 40,
          top: height - 90,
          width: 30,
          height: 30
        },
        clickable: true
      }]
    })
  },
  markertap(e) {
    // console.log(e.markerId)
  },
  controltap(e) {
    var that = this
    // console.log(e.controlId)
    if (e.controlId == 100) {
      that.mapCtx.moveToLocation()
      that.setData({
        controls: [{
          id: 100,
          iconPath: '/image/location_center.png',
          position: {
            left: width - 40,
            top: height - 50,
            width: 30,
            height: 30
          },
          clickable: true
        },
        {
          id: 101,
          iconPath: '/image/refresh.png',
          position: {
            left: width - 40,
            top: height - 90,
            width: 30,
            height: 30
          },
          clickable: true
        }]
      })
    } else {
      that.reFresh()
    }
  },
  onLoad: function (options) {
    var that = this
    // 因为busline页面的刷新后，公交定位坐标有异常，故无法从上页获取busRouteDetail数据
    // var busRouteDetail = JSON.parse(options.busRouteDetail)
    wx.setNavigationBarTitle({
      title: options.lineName
    })
    that.setData({
      upDown: options.upDown,
      lineId: options.lineId,
      lineName: options.lineName
    })
    that.reFresh()
    if (options.lat) {
      var lgtlat = common.wgs84togcj02(parseFloat(options.lgt), parseFloat(options.lat))
      that.setData({
        latitude: lgtlat[1],
        longitude: lgtlat[0],
      })
    }else if (options.latitude) {
      // var lgtlat = common.wgs84togcj02(parseFloat(options.lgt), parseFloat(options.lat))
      that.setData({
        latitude: options.latitude,
        longitude: options.longitude,
      })
    } else {
      wx.getLocation({
        type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
        success: function (res) {
          that.setData({
            latitude: res.latitude,
            longitude: res.longitude,
          })
        }
      })
    }
  },
  onShow: function () {
    if (needRefresh) {
      this.reFresh()
    }
  },
  onHide: function () {
    needRefresh = true
  },
  reFresh: function (e) {
    var that = this
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    })
    wx.request({
      url: common.trafficUrl() + 'transitSearch/lineGps',
      data: {
        lineId: that.data.lineId,
        upOrDown: that.data.upDown,
        longtitude: that.data.longitude,
        latitude:that.data.latitude,
        // sid: -1,
        // order: -1,
        // userId: "",
        // isUpdateCook: 1
      },
      header: {
        token:'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9'
      },
      method: "GET",
      success: function (res) {
        console.log(res)
        if (res.data) {
          var busRouteDetail = res.data
          that.setData({
            busRouteDetail: busRouteDetail
          })
          var points = []
          var markers = []
          for (var i = 0; i < busRouteDetail.relationStations.length; i++) {
            if (busRouteDetail.relationStations[i].parkedBuses.length > 0) {
              for (var j = 0; j < busRouteDetail.relationStations[i].parkedBuses.length; j++) {
                var lgtlat = common.wgs84togcj02(parseFloat(busRouteDetail.relationStations[i].parkedBuses[j].location.longtitude), parseFloat(busRouteDetail.relationStations[i].parkedBuses[j].location.latitude))
                var marker = {
                  iconPath: "/image/red.png",
                  id: 200 + i,
                  latitude: lgtlat[1],
                  longitude: lgtlat[0],
                  title: busRouteDetail.relationStations[i].parkedBuses[j].vehCard,
                  width: 10,
                  height: 10
                }
                markers.push(marker)
              }
            }
          }
          for (var i = 0; i < busRouteDetail.relationStations.length; i = i + 2) {
            var lgtlat = common.wgs84togcj02(busRouteDetail.relationStations[i].location.longtitude, busRouteDetail.relationStations[i].location.latitude)
            var point = {
              longitude: lgtlat[0],
              latitude: lgtlat[1]
            }
            var marker = {}
            if (i == 0) {
              marker = {
                iconPath: "/image/start.png",
                id: busRouteDetail.relationStations[i].id,
                latitude: lgtlat[1],
                longitude: lgtlat[0],
                title: busRouteDetail.relationStations[i].nameOfStation,
                width: 30,
                height: 30
              }
            } else if (i == busRouteDetail.relationStations.length - 1) {
              marker = {
                iconPath: "/image/end.png",
                id: busRouteDetail.relationStations[i].id,
                latitude: lgtlat[1],
                longitude: lgtlat[0],
                title: busRouteDetail.relationStations[i].nameOfStation,
                width: 30,
                height: 30
              }
            } else {
              marker = {
                iconPath: "/image/bus_station.png",
                id: busRouteDetail.relationStations[i].id,
                latitude: lgtlat[1],
                longitude: lgtlat[0],
                title: busRouteDetail.relationStations[i].nameOfStation,
                width: 20,
                height: 20
              }
            }
            points.push(point)
            markers.push(marker)
          }
          var sys = wx.getSystemInfoSync()
          width = sys.windowWidth
          height = sys.windowHeight
          that.setData({
            height: height,
            busRouteDetail: busRouteDetail,
            controls: [{
              id: 100,
              iconPath: '/image/location_center.png',
              position: {
                left: width - 40,
                top: height - 50,
                width: 30,
                height: 30
              },
              clickable: true
            },
            {
              id: 101,
              iconPath: '/image/refresh.png',
              position: {
                left: width - 40,
                top: height - 90,
                width: 30,
                height: 30
              },
              clickable: true
            }],
            polyline: [{
              points: points,
              color: "#743287",
              width: 2,
              dottedLine: false
            }],
            markers: markers
          })
        }
        wx.hideToast()
      }
    })
  },
  onReady: function (e) {
    // 使用 wx.createMapContext 获取 map 上下文 
    this.mapCtx = wx.createMapContext('map')
  }
})